using System;
using System.Collections;
using SecuBat.Threading;
using SecuBat.Crawling;
using SecuBat.Crawling.Forms;

namespace SecuBat.Work
{
	/// <summary>
	/// Summary description for AnalyzeWebPageTask.
	/// </summary>
	public class AnalyzeWebPageTask : Task
	{
		private WebPage _webPage;
		private int _actualDepth;
		private Hashtable _formsAlreadyAttacked;

		// TODO: add locking
		public WebPage WebPage
		{
			get { return _webPage; }
			set { _webPage = value; }
		}

		public int ActualDepth
		{
			get { return LockedCopy(ref _actualDepth); }
			set { LockedSet(ref _actualDepth, ref value); }
		}

		public AnalyzeWebPageTask(WebAddress address, int actualDepth, Hashtable formsAlreadyAttacked, int runId)
		{
			_webPage = new WebPage(address, runId);
			_actualDepth = actualDepth;
			_formsAlreadyAttacked = formsAlreadyAttacked;
            this.Url = address.Url;
		}

		public override void Run()
		{
            this.Status = TaskStatus.Initializing;
            this.TaskActivity = "Initializing task";
            this.SignalizeStatusChange();
			base.Run();
			this.Status = TaskStatus.SendingRequest;
            this.TaskActivity = "Requesting & analyzing URL";
            this.SignalizeStatusChange();
            this.WebPage.ProcessPage(_formsAlreadyAttacked, _actualDepth);
			this.Status = TaskStatus.Finished;
            this.SignalizeStatusChange();
		}

	}
}
